﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AncientLand.Service.Entity;
using AncientLand.Service.IBiz;
using AncientLand.Service.IDa;

namespace AncientLand.Biz
{
   public class NewsBiz:INewsBiz
    {
        private readonly INewsRepository _repository;
        private readonly ICommit _commitAble;
        public NewsBiz(INewsRepository repository, ICommit commitAable)
        {
            this._repository = repository;
            this._commitAble = commitAable;
        }  

       public IEnumerable<News> GetAll()
       {
          return _repository.GetAll();
       }

       public News GetById(int id)
       {
           return _repository.GetById(id);
       }

       public void Create(News news)
       {
           _repository.Add(news);
           Save();
       }

       public void Update(News news)
       {
           _repository.Update(news);
           Save();
       }
       public void Delete(int id)
       {
           var news = _repository.GetById(id);
           _repository.Delete(news);
           Save();
       }

       public void Save()
       {
           _commitAble.Commit();
       }

       public IEnumerable<News> GetNewsByPages(out int totalRecord,int? pageIndex=1, int? pageSize=10, string orderFiled="ID")
       {
           return _repository.GetNewsByPages(pageIndex.GetValueOrDefault(), pageSize.GetValueOrDefault(), orderFiled,out totalRecord);
       }

       public Boolean SetReadTimes(int? id = 1, int? readTimes = 1)
       {
           var result = false;
           result=_repository.SetReadTimes(id.GetValueOrDefault(),readTimes.GetValueOrDefault());
           Save();

           return result;
       }
    }
}
